using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TimerUI : MonoBehaviour
{
    public GameObject timeLimit;
    Text countDown;

    bool isCountDown;
    float countDownTimer;

    public static TimerUI Instance;
    private void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        countDown = timeLimit.transform.GetChild(0).GetComponent<Text>();
        timeLimit.SetActive(false);
        isCountDown = false;
    }

    void Update()
    {
        if (isCountDown)
            CountDownTimer();
    }

    public void SetCountDown(float time)
    {
        if(time == -1) return;

        countDownTimer = time;
        isCountDown = true;
        timeLimit.SetActive(true);
        countDown.text = countDownTimer.ToString("#0.00");
        countDown.color = Color.black;
    }

    void CountDownTimer()
    {
        countDownTimer -= Time.deltaTime;
        if (countDownTimer < 0)
        {
            countDownTimer = 0;
            isCountDown = false;
            timeLimit.SetActive(false);
        }
        countDown.text = countDownTimer.ToString("#0.00");
        if (countDownTimer < 2f)
            countDown.color = Color.red;
    }

    public void CloseTimeLimit()
    {
        isCountDown = false;
        timeLimit.SetActive(false);
    }
}
